package edu.gyc.mytest;

import javax.swing.*;
import java.awt.*;

class MyPanel extends JPanel{
    private int x=200;
    private int y=200;

    public void display() {
        x++;
        y++;

        if (y > 520) {
            y=200;
        }
        if (x > 740) {
            x=200;
        }
        System.out.println("y="+y);
        this.repaint();
    }


    @Override
    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D graphics2D=(Graphics2D)g;
        graphics2D.setColor(Color.red);
        graphics2D.fill3DRect(x,y,60,80,true);
    }
}

public class PanelTest {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        MyPanel myPanel=new MyPanel();
        //jFrame.setSize(800,600);
        jFrame.setBounds(200,300,800,600);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     //   jFrame.setContentPane(myPanel);
        jFrame.add(myPanel);
        jFrame.setVisible(true);
        System.out.println("JPanel: "+myPanel.getSize());
        System.out.println("JFrame:"+jFrame.getSize());

        while (true) {
            myPanel.display();
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
